package com.yzk.auction.dao;

import com.yzk.auction.entity.Bidding;
import com.yzk.auction.entity.OrderAll;
import com.yzk.auction.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

public class BiddingDao {

    /**
     * 显示全部竞拍
     * */
    public static List<Bidding> query(int id){
        DataSource dataSource = JDBCUtils.getDataSource();
        QueryRunner qr = new QueryRunner(dataSource);
        List<Bidding> list = null ;
        try {
            list = qr.query("select * from tb_bidding where goods_id="+id+" ORDER BY bidding_price DESC;",
                    new BeanListHandler<>(Bidding.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static long biddingCount(){
        long count = 0;
        DataSource dataSource = JDBCUtils.getDataSource();
        QueryRunner qr = new QueryRunner(dataSource);
        try {
            count = (long) qr.query("SELECT COUNT(*) FROM tb_bidding;",
                    new ScalarHandler());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    public static void insert(long bidding_id, int bidding_status, int user_id, double bidding_price, Date bidding_time, int goods_id) throws SQLException {
        //bidding表添加sql
        QueryRunner runner = new QueryRunner(JDBCUtils.getDataSource());
        long time = bidding_time.getTime();
        Timestamp timestamp = new Timestamp(time);
        runner.update("INSERT INTO tb_bidding VALUES(?,?,?,?,?,?)",bidding_id,bidding_status,user_id,bidding_price,bidding_time,goods_id);
    }
    public void updateStatus(int goods_id) throws SQLException {
        QueryRunner runner = new QueryRunner(JDBCUtils.getDataSource());
        runner.update("UPDATE tb_bidding set bidding_status=0 where goods_id=?",goods_id);
    }
}
